const App = getApp()
import { request } from '../../request/index'
//使用async await需要引入，只引入就行
import regeneratorRuntime from '../../lib/runtime/runtime'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goods_info: {},
    isCollection: false,
    streamlineGoodsInfo:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    console.log(options)
    await this.getPageSource(parseInt(options.id))
    this.streamlineGoodsInfo()
    this.goodIsCollection()

  },

  //判断该商品是否被收藏
  goodIsCollection(){
    const result = wx.getStorageSync('collection_goods') || [];
    const result2 = result.findIndex(item => 
      item.goods_id === this.data.goods_info.goods_id)
    if(result2 !== -1){
      this.setData({isCollection:true})
    }

  },

  //精简goodsInfo传值
  streamlineGoodsInfo(){
    const { goods_id,goods_small_logo,goods_price,goods_name }
     = this.data.goods_info
    let streamlineGoodsInfo = 
    { goods_id,goods_small_logo,goods_price,goods_name }
    this.setData({streamlineGoodsInfo})
  },

  //collectionGoods收藏商品
  collectionGoods(event) {
    console.log('收藏', event)
    const {goodsinfo} = event.currentTarget.dataset
    const { isCollection } = this.data
    const collection_goods = wx.getStorageSync('collection_goods') || [];

    this.setData({ isCollection: !isCollection }, () => {
      if(this.data.isCollection){
        collection_goods.push(goodsinfo)
        App.globalData.collectionCount++
      }else{
        const index = collection_goods.findIndex(item => 
          item.goods_id === goodsinfo.goods_id)
        collection_goods.splice(index,1)
        App.globalData.collectionCount--
      }
      wx.setStorageSync('collection_goods', collection_goods);
      
    })
  },

  //获取页面数据
  async getPageSource(id) {
    const result = await request({ url: '/goods/detail', data: { goods_id: id } })
    console.log(result)
    const {
      goods_name, goods_price, goods_small_logo, goods_id,
      goods_introduce, pics
    } = result.data.message
    const goods_info = {
      goods_name, goods_price,
      goods_introduce, pics, goods_small_logo, goods_id
    }
    this.setData({ goods_info })
  },

  //处理预览图片
  handlePreviewImage(event) {
    const { pics } = this.data.goods_info
    const urls = pics.map(item => item.pics_mid)
    const current = event.currentTarget.dataset.url
    // console.log(current, event)
    wx.previewImage({
      current, // 当前显示图片的http链接
      urls // 需要预览的图片http链接列表
    })
  },
  //加入购物车事件
  addCart() {
    //获取当前物品id
    const goods_info = this.data.goods_info
    //从本地取出
    const result = wx.getStorageSync('cart') || []
    let goods_index = result.findIndex(item =>
      item.goods_id === goods_info.goods_id)
    // console.log(goods_index)
    if (goods_index === -1) {
      goods_info.count = 1
      result.push(goods_info)
    } else {
      result[goods_index].count++
    }
    wx.setStorageSync('cart', result);
    wx.showToast({
      title: '加入成功',
      icon: 'success',
      image: '',
      duration: 1000,
      mask: true,
    });
  },

  //立即购买事件
  nowPay() {
    wx.switchTab({
      url: '/pages/cart/cart'
    });
  },


})